package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import entity.Matriculado;

import BD.ConnectionFactory;

public class MatriculadosDAO {
	Connection connection = null;
	PreparedStatement ptmt = null;
	ResultSet resultSet = null;
	
	public MatriculadosDAO(){
		
	}
	
	private Connection getConnection() throws SQLException {
		Connection con;
		con = ConnectionFactory.getInstance().getConnection();
		return con;
	}
	
	public void add(Matriculado matriculado){
		String queryString ="INSERT INTO matriculado(alumno, asignatura) VALUES (?, ?)";
		try {
			connection = getConnection();
			ptmt = connection.prepareStatement(queryString);
			ptmt.setInt(1, matriculado.getAlumno());
			ptmt.setInt(2, matriculado.getAsignatura());
			ptmt.executeUpdate();
			System.out.println("Filas anyadidas con exito");
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
			if(ptmt != null)
				ptmt.close();
			if (connection != null)
				connection.close();
			} catch (SQLException e) {
					e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	
		
	}
	
	public void update(Matriculado matriculado) {
		String queryString = "UPDATE matriculado SET asignatura=? WHERE alumno=?";
		try {
			connection = getConnection();
			ptmt = connection.prepareStatement(queryString);
			ptmt.setInt(2, matriculado.getAlumno());
			ptmt.setInt(1, matriculado.getAsignatura());
			ptmt.executeUpdate();
			System.out.println("Filas modificadas con exito");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			try {
			if(ptmt != null)
				ptmt.close();
			if (connection != null)
				connection.close();
			} catch (SQLException e) {
					e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	public void delete(Matriculado matriculado){
		String queryString = "DELETE FROM matriculado WHERE alumno=?";
		try {
			connection = getConnection();
			ptmt = connection.prepareStatement(queryString);
			ptmt.setInt(1, matriculado.getAlumno());
			ptmt.executeUpdate();
			System.out.println("Filas eliminadas con exito");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(ptmt != null)
					ptmt.close();
				if (connection != null)
					connection.close();
				} catch (SQLException e) {
						e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			
		}
	}
	
	public void findAll() {
		String queryString = "SELECT * FROM matriculado";
		try {
			connection = getConnection();
			ptmt = connection.prepareStatement(queryString);
			resultSet = ptmt.executeQuery();
			while(resultSet.next()) {
				System.out.println("Alumno " +resultSet.getInt("alumno")+", Asignatura "+ resultSet.getInt("asignatura"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			try {
				if(ptmt != null)
					ptmt.close();
				if (connection != null)
					connection.close();
				} catch (SQLException e) {
						e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			
		}
	}
}
